local qinxin = fk.CreateSkill{
  name = "dajubei__qinxin",
  tags = { Skill.Switch },
  dynamic_desc = function (self, player)
    local n = player:getMark(self.name) + 1
    local tail = {}
    for i = 1, 8, 1 do
      table.insert(tail, Fk:translate("#dajubei__qinxin_" .. i))
    end
    tail[n] = "<font color=\"#E0DB2F\">" .. tail[n] .. "</font>"
    return "dajubei__qinxin_inner:" .. table.concat(tail, ":")
  end
}

Fk:loadTranslationTable{
  ["dajubei__qinxin"] = "侵心",
  [":dajubei__qinxin"] = "转换技，当一名角色受到伤害后，你可以"
  .."①令其回复1点体力；②令其摸两张牌；③摸一张牌；④什么也不做；"
  .."⑤弃置其两张牌；⑥弃置一张牌；⑦对其造成1点伤害；"
  .."⑧失去“侵心”，获得“暗煞”，获得【霜之哀伤】并使用之。",

  [":dajubei__qinxin_inner"] = "转换技，当一名角色受到伤害后，你可以{1}{2}{3}{4}{5}{6}{7}{8}",
  ["#dajubei__qinxin_1"] = "①令其回复1点体力；",
  ["#dajubei__qinxin_2"] = "②令其摸两张牌；",
  ["#dajubei__qinxin_3"] = "③摸一张牌；",
  ["#dajubei__qinxin_4"] = "④什么也不做；",
  ["#dajubei__qinxin_5"] = "⑤弃置其两张牌；",
  ["#dajubei__qinxin_6"] = "⑥弃置一张牌；",
  ["#dajubei__qinxin_7"] = "⑦对其造成1点伤害；",
  ["#dajubei__qinxin_8"] = "⑧失去“侵心”，获得“暗煞”，获得【霜之哀伤】并使用之。",

  ["dajubei__qinxin1"] = "侵心：可以令 %src 回复1点体力",
  ["dajubei__qinxin2"] = "侵心：可以令 %src 摸两张牌",
  ["dajubei__qinxin3"] = "侵心：可以摸一张牌",
  ["dajubei__qinxin4"] = "侵心：可以什么也不做",
  ["dajubei__qinxin5"] = "侵心：可以弃置 %src 两张牌",
  ["dajubei__qinxin6"] = "侵心：可以弃置一张牌",
  ["dajubei__qinxin7"] = "侵心：可以对 %src 造成1点伤害",
  ["dajubei__qinxin8"] = "侵心：可以失去“侵心”并获得“暗煞”， 获得【霜之哀伤】并使用之",

  ["$dajubei__qinxin1"] = "圣光，请指引我。",
   ["$dajubei__qinxin2"] = "我可以帮你。",
   ["$dajubei__qinxin3"] = "我为圣光而战。",
   ["$dajubei__qinxin4"] = "我知道自己在干什么。",
   ["$dajubei__qinxin5"] = "圣光的叛徒！",
   ["$dajubei__qinxin6"] = "王子并不好当。",
   ["$dajubei__qinxin7"] = "你已无药可救！",
   ["$dajubei__qinxin8"] =  "相信圣光的我真是愚蠢。",
}

---@param player ServerPlayer
---@param target ServerPlayer
---@param index integer
local qinxinEffcts = function (player, target, index)
  local room = player.room
  local effects = {
    function ()
      if target:isWounded() then
        room:recover{
          num = 1,
          who = target,
          recoverBy = player,
          skillName = "dajubei__qinxin",
        }
      end
    end,
    function ()
      if target:isAlive() then
        target:drawCards(2, "dajubei__qinxin")
      end
    end,
    function ()
      if player:isAlive() then
        player:drawCards(1, "dajubei__qinxin")
      end
    end,
    function ()
      -- do nothing
    end,
    function ()
      if #target:getCardIds("he") >= 2 then
        local cards = room:askToChooseCards(player, {
          min = 2,
          max = 2,
          target = target,
          flag = "he",
          skill_name = "dajubei__qinxin",
        })
        room:throwCard(cards, "dajubei__qinxin", target, player)
      end
    end,
    function ()
      if not player:isNude() then
        room:askToDiscard(player, {
          min_num = 1,
          max_num = 1,
          skill_name = "dajubei__qinxin",
          cancelable = false,
          include_equip = true,
        })
      end
    end,
    function ()
      if target:isAlive() then
        room:damage{
          damage = 1,
          to = target,
          from = player,
          skillName = "dajubei__qinxin",
        }
      end
    end,
    function ()
      if player:isAlive() then
        room:handleAddLoseSkills(player, "-dajubei__qinxin")

        if player.general == "djb1__arthas" then
          player.general = "djb2__arthas"
          player.room:broadcastProperty(player, "general")
        end
        if player.deputyGeneral == "djb1__arthas" then
          player.deputyGeneral = "djb2__arthas"
          player.room:broadcastProperty(player, "deputyGeneral")
        end
        room:handleAddLoseSkills(player, "dajubei__ansha")
        local death_knights = player:getPile("ansha_death_knights")
        if #death_knights > 0 then
          local to_gain = table.filter(death_knights, function (id)
            return Fk:getCardById(id).name == "frostmourne"
          end)
          room:obtainCard(player, to_gain, true, fk.ReasonJustMove, player, "dajubei__qinxin")
          if table.contains(player:getCardIds("h"), to_gain[1]) then
            room:useCard {
              from = player,
              tos = { player },
              card = Fk:getCardById(to_gain[1]),
            }
          end
        end
      end
    end,
  }

  if index < 8 then player.room:addPlayerMark(player, qinxin.name, 1) end
  effects[index]()
end

qinxin:addLoseEffect(function (self, player, is_start)
  player.room:setPlayerMark(player, qinxin.name, 0)
end)

qinxin:addEffect(fk.Damaged, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill("dajubei__qinxin") then
      local n = player:usedSkillTimes("dajubei__qinxin", Player.HistoryGame) + 1
      return (n == 1 and target:isWounded())
      or (table.contains({2, 7}, n) and target:isAlive())
      or table.contains({3, 4, 8}, n)
      or (n == 5 and #target:getCardIds("he") >= 2)
      or (n == 6 and not player:isNude())
    end
  end,
  on_cost = function (self, event, target, player, data)
    local n = player:usedSkillTimes("dajubei__qinxin", Player.HistoryGame) + 1
    local prompt = "dajubei__qinxin"..tostring(n)
    if table.contains({1, 2, 5, 7}, n) then
      prompt = prompt .. ":" .. target.id
    end
    return player.room:askToSkillInvoke(player, {
      skill_name = "dajubei__qinxin",
      prompt = prompt,
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = player:usedSkillTimes("dajubei__qinxin", Player.HistoryGame)
    room:notifySkillInvoked(player, "dajubei__qinxin", n < 8 and "switch" or "big")
    player:broadcastSkillInvoke("dajubei__qinxin", n)
    qinxinEffcts(player, target, n)
  end
})

return qinxin